//
//  GenearchList.m
//  Eath
//
//  Created by mao on 11/6/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "GenearchList.h"
#import "User.h"
#import "Tool.h"

@implementation GenearchList

- (id) init
{
    self = [super init];
    if (self != nil) {
        //根据当前登录用户获取副家长信息
        self.purpose = 1;
        
        
        self.dataList = [[NSMutableArray alloc] init];
        
//        NSUserDefaults * userdefualts = [NSUserDefaults standardUserDefaults];
//        NSDictionary * gen_list_mes = [userdefualts dictionaryForKey:@"gens_list"];
        
//        if(YES || gen_list_mes == nil){
            [self network_gens];
//        }else{
//            NSInteger expires = [gen_list_mes[@"expires"] integerValue];
//            NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0];
//            NSTimeInterval date = [dat timeIntervalSince1970];
//            
//            if((int)date > (int)expires){
//                //过期
//                [self network_gens];
//                
//            }else{
//                //未过期
//                self.dataList = (NSMutableArray *)gen_list_mes[@"data"];
//                self.GenList = [self getGenearchList];
//                [self.detegate gens_load_over];
//            }
//        }
    }
    return self;
}

- (void) network_gens
{

    if(![User isLogin]){
        [Tool showMessage:@"请登录!"];
        return;
    }
    NSString * token = [User token];
    NSDictionary * userinfo = [User loginInfo];
    DLog(@"%@",userinfo);
    NSMutableDictionary * login_user = [[NSMutableDictionary alloc] init];
    login_user[@"id"] = userinfo[@"id"];
    login_user[@"name"] = userinfo[@"username"];
    login_user[@"card"] = userinfo[@"id_card"];
    
    login_user[@"phone"] = userinfo[@"phone"];
    login_user[@"photo"] = @"default_head";
    login_user[@"sex"] = userinfo[@"gender"];
    login_user[@"full_name"] = userinfo[@"name"];
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString * url_stus_get = [NSString stringWithFormat:@"%@?id=%@&token=%@",URL_VICE_USER,userinfo[@"id"],token];
    DLog(@"%@",url_stus_get);
    [manager GET:url_stus_get parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * redata = responseObject;
        NSArray * data = redata[@"data"];
        NSString * code = redata[@"code"];
        
        if(code.intValue  == 0){
            
            NSMutableArray * gen_list = [[NSMutableArray alloc] init];
            [gen_list addObject:login_user];
            
                DLog(@"%@",redata);
            int count = 0;
            for (NSDictionary * gen in data) {
                NSMutableDictionary * tmp = [[NSMutableDictionary alloc] init];
                
                tmp[@"id"] = gen[@"uid"];
                tmp[@"name"] = gen[@"name"];
                tmp[@"card"] = @"";
                tmp[@"pwd"] = @"";
                tmp[@"phone"] = gen[@"phone"];
                tmp[@"photo"] = @"default_head";
                tmp[@"sex"] = @"";
                tmp[@"full_name"] = gen[@"name"];
                
                [gen_list addObject:tmp];
                count ++;
            }
            self.dataList = gen_list;
//            NSMutableDictionary * gens_list_cache = [NSMutableDictionary dictionaryWithCapacity:2];
//            NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0];
//            NSTimeInterval date = [dat timeIntervalSince1970];
//            gens_list_cache[@"expires"] = [NSNumber numberWithDouble:(date + 60*10)];
//            gens_list_cache[@"data"] = gen_list;
//            NSUserDefaults * userdefualts = [NSUserDefaults standardUserDefaults];
//            [userdefualts setObject:gens_list_cache forKey:@"gens_list"];
            
            self.GenList = [self getGenearchList];
//            DLog(@"%@",self.GenList);
            [self.detegate gens_load_over];
            
        }else{
            [self.detegate gens_load_fail];
            [Tool showMessage:redata[@"msg"]];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [Tool showMessage:@"家长获取失败!"];
        [self.detegate gens_load_fail];
    }];
}

- (id) initWithStuId:(NSString *)stuId
{
    self = [super init];  // Call a designated initializer here.
    if (self != nil) {
        self.purpose = 2;
        
        self.dataList = [[NSMutableArray alloc] init];
        
        NSUserDefaults * userdefualts = [NSUserDefaults standardUserDefaults];
        NSDictionary * gen_list_mes = [userdefualts dictionaryForKey:@"gens_list"];
        
        if(gen_list_mes == nil){
            [self network_gens];
        }else{
            NSInteger expires = [gen_list_mes[@"expires"] integerValue];
            NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0];
            NSTimeInterval date = [dat timeIntervalSince1970];
            
            if((int)date > (int)expires){
                //过期
                [self network_gens];
                
            }else{
                //未过期
                self.dataList = (NSMutableArray *)gen_list_mes[@"data"];
                self.GenList = [self getGenearchList];
                [self.detegate gens_load_over];
            }
        }
    }
    return self;
}

- (NSMutableArray *) getGenearchList
{
    if (self.purpose == 1) {
        
        NSMutableArray * data = [[NSMutableArray alloc] init];
        for (int i = 0; i < [self.dataList count]; i++) {
            Genearch * gen = [[Genearch alloc] initWithId:self.dataList[i][@"id"] andName:self.dataList[i][@"name"] andRelation:@"" andPhoto:self.dataList[i][@"photo"] andCard:self.dataList[i][@"card"] andPhone:self.dataList[i][@"phone"] andMain:0 andSex:[self.dataList[i][@"id"] integerValue] andFullName:self.dataList[i][@"full_name"] andPwd:self.dataList[i][@"pwd"]];
            data[i] = gen;
        }
        return data;
        
    }else if (self.purpose == 2){
        
        NSMutableArray * data = [[NSMutableArray alloc] init];
        for (int i = 0; i < [self.dataList count]; i++) {
            Genearch * gen = [[Genearch alloc] initWithId:self.dataList[i][@"id"] andName:self.dataList[i][@"name"] andRelation:self.dataList[i][@"relation"] andPhoto:self.dataList[i][@"photo"] andCard:self.dataList[i][@"card"] andPhone:self.dataList[i][@"phone"] andMain:[self.dataList[i][@"main"] boolValue] andSex:[self.dataList[i][@"id"] integerValue] andFullName:self.dataList[i][@"full_name"] andPwd:self.dataList[i][@"pwd"]];
            data[i] = gen;
        }
        return data;
    }else{
        return [[NSMutableArray alloc] init];
    }
}
@end
